/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.Session;
import com.mol.market.common.widget.NavigationTitle;
import com.mol.market.fanli.ui.MakeMoneyActivity;
import com.mol.market.ui.SearchActivity;

/**
 * TopBar utility class
 * 
 * @author alex
 * @date 2011-1-1
 * @since Version 0.4.0
 * 
 */
public class TopBar {

  public static void initSkin(final Session session, final Activity context) {

    View bg = context.findViewById(R.id.top_bar);
    bg.setBackgroundResource(ThemeManager.getResource(session,
        ThemeManager.KEY_TOP_BAR_BG));

    ImageView logo = (ImageView) context.findViewById(R.id.logo);
    logo.setImageResource(ThemeManager.getResource(session,
        ThemeManager.KEY_TOP_BAR_LOGO));

    ImageButton search = (ImageButton) context
        .findViewById(R.id.top_bar_search);
    search.setImageResource(ThemeManager.getResource(session,
        ThemeManager.KEY_TOP_BAR_SEARCH));
    search.setBackgroundResource(ThemeManager.getResource(session,
        ThemeManager.KEY_TOP_BAR_BTN_BG));

    NavigationTitle title = (NavigationTitle) context
        .findViewById(R.id.top_bar_title);
    title.initSkin(session.getTheme());
  }

  /**
   * 更改指定控件的content
   * 
   * @param activity
   * @param Id
   * @param content
   */
  public static void changeText(final Activity activity, final String content) {
    TextView view = (TextView) activity.findViewById(R.id.top_bar_tv_money);
    view.setText(content);
  }

  public static void createTopBar(final Session session,
      final Activity context, View[] views, int[] visibility, String... title) {

    View bg = context.findViewById(R.id.top_bar);
    bg.setBackgroundResource(ThemeManager.getResource(session,
        ThemeManager.KEY_TOP_BAR_BG));

    String tempTitle = null;
    int titleCount = title.length;
    int nowCount = 0;

    final int size = views.length;
    for (int i = 0; i < size; i++) {
      // set visibility
      View v = views[i];
      v.setVisibility(visibility[i]);

      if (View.GONE == visibility[i]) {
        // no need to assign values
        continue;
      }

      // Tip：如果传入的String够用，就用传入的String，如果不够用，就让String为字符串“null"
      if (nowCount < titleCount) {
        tempTitle = title[nowCount];
      } else {
        tempTitle = null;
      }

      switch (v.getId()) {

        case R.id.logo:

          ImageView logo = (ImageView) context.findViewById(R.id.logo);
          logo.setImageResource(ThemeManager.getResource(session,
              ThemeManager.KEY_TOP_BAR_LOGO));
          break;

        case R.id.top_bar_search:

          ImageButton search = (ImageButton) v;
          search.setImageResource(ThemeManager.getResource(session,
              ThemeManager.KEY_TOP_BAR_SEARCH));
          
          // go to search activity
          search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
              Intent intent = new Intent(context, SearchActivity.class);
//              Intent intent = new Intent(context, GfanFragmentActivity.class);
              context.startActivity(intent);
            }
          });
          break;

        case R.id.top_bar_rebate:

          ImageButton rebate = (ImageButton) v;
          // 进入返利主页
          rebate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
              Utils.trackEvent(context, Constants.GROUP_REBATE,
                  Constants.REBATE_ENTRANCE_1);
              Intent intent = new Intent(context, MakeMoneyActivity.class);
              context.startActivity(intent);
            }
          });
          break;

        case R.id.top_bar_btn_exchange:

          Button exchange = (Button) v;
          exchange.setText(tempTitle);
          exchange.setBackgroundResource(ThemeManager.getResource(session,
              ThemeManager.KEY_TOP_BAR_EXCHANGE));
          exchange.setShadowLayer(0.1f, 0, -2.0f, Color.BLACK);
          nowCount++;
          break;

        case R.id.top_bar_title:
          ((NavigationTitle) v).pushTitle(tempTitle);
          nowCount++;
          break;
      }
    }
  }

}